package com.asset.dispoal.address.service.commom;

import com.asset.dispoal.address.domain.db.entity.Area;
import com.asset.dispoal.address.domain.db.mapper.AreaMapper;
import com.asset.dispoal.address.dto.AreaRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.core.spring.util.DataUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;


@Component
public class AreaUpdateCommand implements BaseCommand<AreaRequest, Result> {

    @Autowired
    AreaMapper areaMapper;

    @Override
    public Result execute(AreaRequest request) {
        Area area = DataUtil.copyTo(request, Area.class);
        Area check = areaMapper.check(request.getOldName(), request.getNewCode());
        if (check != null && StringUtil.notEquals(check.getId(), request.getId())) {
            return Result.faild("修改的数据在解析里存在");
        }
        areaMapper.update(area);
        return Result.ok();
    }
}
